/**
 * Created by tdzl2003 on 2/25/16.
 */
const webpack = require('webpack');
const path = require('path');

const ExtractTextPlugin = require('extract-text-webpack-plugin');

const __DEV__ = process.argv.indexOf('--release') < 0;

const assetsPath = path.join(__dirname, __DEV__ ? 'build-debug' : 'build-release');

const autoprefixer = require('autoprefixer');

function styleLoader(type) {
  const loaders = [
    'style',
    `css?${JSON.stringify({
      module: true,
      importLoaders: 2,
      sourceMap: __DEV__,
    })}`,
    'postcss',
  ];

  if (type) {
    loaders.push(`${type}?${JSON.stringify({
      outputStyle: 'expanded',
      sourceMap: __DEV__,
    })}`);
  }

  if (__DEV__) {
    return loaders.join('!');
  }
  return ExtractTextPlugin.extract(loaders[0], loaders.slice(1));
}

module.exports = {
  devtool: __DEV__ && 'inline-source-map',
  context: __dirname,
  entry: {
    'index': __DEV__ ? [
      'webpack-hot-middleware/client?path=/__webpack_hmr',
      './src/index',
    ] : [
      './src/index',
    ],
  },
  output: {
    path: assetsPath,
    filename:  '[name].bundle.js',
    chunkFilename: '[name]-[chunkhash].chunk.js',
    publicPath: '/scripts/',
  },
  postcss: () => [autoprefixer],
  module: {
    loaders: [
      { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel'},
      { test: /\.json$/, loader: 'json-loader' },
      { test: /\.less$/, loader: styleLoader('less') },
      { test: /\.css$/, loader: styleLoader() },
      { test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff' },
      { test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff' },
      { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream' },
      { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
      { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml' },
      { test: /\.(png|jpg|gif)$/, loader: 'url?limit=10240' },
    ],
  },
  progress: true,
  resolve: {
    modulesDirectories: [
      'src',
      'node_modules',
    ],
    extensions: ['', '.web.js', '.json', '.js']
  },
  plugins: [
    new webpack.DefinePlugin({
      __CLIENT__: true,
      __DEV__: !!__DEV__,
      'process.env': {
        // Useful to reduce the size of client-side libraries, e.g. react
        NODE_ENV: __DEV__ ? JSON.stringify('development') : JSON.stringify('production'),
      },
    }),
    new webpack.ProvidePlugin({
      'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch'
    })
  ].concat(__DEV__ ? [
    // hot reload
    new webpack.HotModuleReplacementPlugin(),
    new webpack.IgnorePlugin(/webpack-stats\.json$/),
  ] : [
    new ExtractTextPlugin('[name].css', {allChunks: true}),
    new webpack.IgnorePlugin(/\.\/dev/, /\/config$/),

    // optimizations
    new webpack.optimize.DedupePlugin(),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      },
    }),
  ]),
  devServer: {
    contentBase: './content/',
    hot: true,
    historyApiFallback: true,
    proxy: {
      '/api/*': {
        target: 'http://localhost:3000/',
        secure: false,
        pathRewrite:{
          '/api' : ''
        }
      }
    },
    quiet: false,
    noInfo: false,
    lazy: true,
    filename: "[name].bundle.js",
    watchOptions: {
      aggregateTimeout: 300,
      poll: 1000
    },
    publicPath: "/scripts/",
    stats: { colors: true },
  },
}
